<template>
  <div class="roles-container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="always" class="box-card">
        <el-button @click="addRoles" type="primary">添加角色</el-button>
         <el-table
          :stripe="true"
          :border="true"
          :data="tableData"
          style="width: 100%"
        >
        <el-table-column
          type="expand"
          width="50">
          <template slot-scope="props">
            <!--            第一层-->
            <el-divider></el-divider>
            <el-row inline class="bdbottom center demo-table-expand" type="flex" :gutter="20"
                    v-for="item in props.row.children"
                    :key="item.id">
              <el-col :span="8" justify="center">
                <el-tag>{{ item.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="16">
                <!--            第二层-->
                <el-row class="center" v-for="item in item.children" :key="item.id" type="flex" align="center"
                        :gutter="20">
                  <el-col :span="8" type="flex" align="center">
                    <el-tag type="success">{{ item.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="16">
                    <!--            第三层-->
                         <el-tag
                            closable
                            v-for="item in item.children"
                            :key="item.id"
                            type="warning"
                            @close="handleClose(props,item.id)"
                          >
                            {{ item.authName }}
                          </el-tag>
                  </el-col>
                </el-row>
               <el-divider></el-divider>
              </el-col>
            </el-row>
            <el-divider></el-divider>
          </template>
        </el-table-column>
        <el-table-column
          type="index"
          label="#"
          width="50">
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="角色名称">
        </el-table-column>
        <el-table-column
          prop="roleDesc"
          label="角色描述">
        </el-table-column>
        <el-table-column
          prop="address"
          width="300"
          label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              class="el-icon-edit"
              @click="handleEdit(scope.row)">&nbsp;编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              class="el-icon-delete"
              @click="removeRole(scope.$index,scope.row)"
            >&nbsp;删除
            </el-button>
            <el-button
              size="mini"
              type="warning"
              class="el-icon-setting"
              @click="dialogvisible(scope.$index, scope.row)">&nbsp;分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="分配权限"
      :visible.sync="dialogVisible"
      :destroy-on-close="true"
      width="50%"
      :before-close="handleCloseDialog"
      @click="handleCloseDialog">
      <el-tree
        :data="powerList"
        show-checkbox
        node-key="id"
        ref="tree"
        :default-expand-all="true"
        :default-checked-keys="id"
        :props="defaultProps">
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="userAuthorization()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加角色 -->
    <el-dialog title="添加角色" :visible.sync="isShowAddRoles">
      <el-form ref="form" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="roles.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roles.roleDesc"></el-input>
        </el-form-item>
      </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="isShowAddRoles = false">取 消</el-button>
          <el-button type="primary" @click="onSaveaAddRoles">确 定</el-button>
        </div>
    </el-dialog>

    <!-- 编辑角色 -->
    <el-dialog title="编辑角色" :visible.sync="isShowEditRoles">
      <el-form ref="form" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="editRoles.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="editRoles.roleDesc"></el-input>
        </el-form-item>
      </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="isShowEditRoles = false">取 消</el-button>
          <el-button type="primary" @click="onSaveaAEditRoles">确 定</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAllRoles, getRolesList, roleAuthorization, deleteRoleAuthorization, deleteRole, addRole, editRole } from '@/api/roles.js'
export default {
  name: 'Roles',
  data () {
    return {
      tableData: [],
      powerList: [],
      id: [],
      userId: '',
      editRolesId: '',
      dialogVisible: false,
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      isShowAddRoles: false,
      isShowEditRoles: false,
      roles: {
        roleName: '',
        roleDesc: ''
      },
      editRoles: {
        roleName: '',
        roleDesc: ''
      }
    }
  },
  created () {
    this.GetUserList()
  },
  methods: {
    async GetUserList () {
      const { data: { data: res } } = await getRolesList()
      this.tableData = res
    },
    async getAllPower () {
      const { data: { data: res } } = await getAllRoles('tree')
      this.powerList = res
    },
    async dialogvisible (index, row) {
      this.userId = row.id
      this.dialogVisible = true
      this.GetUserList()
      this.getAllPower()
      const arr = []
      this.tableData[index].children.forEach(item => {
        item.children.forEach(item => {
          item.children.forEach(item => {
            arr.push(item.id)
          })
        })
      })
      this.id = arr
    },
    async userAuthorization () {
      const keys = this.$refs.tree.getCheckedKeys()
      const rids = keys.toString()
      this.dialogVisible = false
      await roleAuthorization(this.userId, rids)
      this.GetUserList()
      this.$message({
        type: 'success',
        message: '更新成功!'
      })
    },
    handleEdit (row) {
      console.log(row)
      this.editRolesId = row.id
      this.isShowEditRoles = true
      this.editRoles.roleName = row.roleName
      this.editRoles.roleDesc = row.roleDesc
    },
    async onSaveaAEditRoles () {
      await editRole(this.editRoles, this.editRolesId)
      this.isShowEditRoles = false
      this.GetUserList()
      this.$message({
        type: 'success',
        message: '修改成功!'
      })
    },
    removeRole (index, row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteRole(row.id)
        this.tableData.splice(index, 1)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleClose (props, id) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: { data: res } } = await deleteRoleAuthorization(props.row.id, id)
        this.tableData[props.$index].children = res
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleCloseDialog () {
      this.dialogVisible = false
    },
    addRoles () {
      console.log('添加')
      this.isShowAddRoles = true
    },
    async onSaveaAddRoles () {
      await addRole(this.roles)
      this.$message({
        type: 'success',
        message: '添加成功!'
      })
      this.GetUserList()
      this.isShowAddRoles = false
    }
  }
}
</script>

<style lang="less" scoped>
.roles-container{
  .text {
    font-size: 14px;
  }

  .item {
    padding: 18px 0;
  }

  .box-card {
    width: 100%;
  }
}
</style>
